<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
/**
 *    后台管理人员 后台控制
 * @author xiahuiqin
 *
 */
class Admin extends Admin_Controller
{
    public function  __construct()
    {
        parent::__construct();

        $this->load->model("admin_mdl", 'admin');
    }

    public function  index()
    {
        $this->admin_list();
    }

    /**
     * 后台管理员列表
     */
    function admin_list()
    {
        $where  = '';
        $limit  = '';
        $offset = '';
        $order  = 'id ASC';

        $data['result'] = $this->admin->get_list($where, $limit, $offset, $order);
        $this->layout->view('admin/admin/admin_listV', $data);
    }

    /**
     * 新增管理员
     */
    function admin_add()
    {
        $datas['error'] = '';

        if ($_POST) {
            $this->form_validation->set_rules('name', '用户名', 'trim|required|max_length[15]');
            $this->form_validation->set_rules('password', '密码', 'trim|required');
            $this->form_validation->set_rules('cpassword', '确认密码', 'trim|required|matches[password]');
            $this->form_validation->set_rules('authority', '管理员权限', 'required');
            $this->form_validation->set_rules('position', '管理位置', 'required');
            $this->form_validation->set_rules('url', '链接', 'prep_url|valid_url');
            $this->form_validation->set_rules('email', '邮箱', 'valid_email');

            if ($this->form_validation->run() == FALSE) {
                $this->layout->view('admin/admin/admin_addV', $datas);
            } else {
                $datas             = $this->_post_data();
                $datas['reg_time'] = date('Y-m-d H:i:s');

                if ($this->admin->insert($datas)) {
                    $datas['error'] = "新增管理员成功！";
                } else {
                    $datas['error'] = '新增失败！';
                }
                $this->layout->view('admin/admin/admin_addV', $datas);
            }
        } else {
            $this->layout->view('admin/admin/admin_addV', $datas);
        }


    }

    /**
     * 管理员编辑
     */
    function admin_edit()
    {
        $id = $this->uri->segment('4');

        if ($_POST) {
            $datas             = $this->_post_data();
            $datas['reg_time'] = $this->input->post('reg_time');

            if ($this->admin->update_byfield('id', $id, $datas)) {
                $data['error'] = "更新管理员信息成功！";
            } else {
                $data['error'] = '更新失败！';
            }
        } else {
            $data['error'] = '';
        }
        $data['one'] = $this->admin->get_one_list($id);

        $this->layout->view('admin/admin/admin_editV', $data);
    }

    /**
     * 管理员删除
     */
    function admin_delete()
    {
        $id  = $this->uri->segment(4);
        $con = array('id' => $id);

        $this->admin->del($con);

        $this->admin_list();
    }

    function _post_data()
    {
        $datas['name']  = $this->input->post('name'); //用户名
        $datas['phone'] = $this->input->post('phone'); //联系电话
        $datas['email'] = $this->input->post('email'); //邮箱
        $datas['url']   = $this->input->post('url'); //个人链接
        if (strlen($this->input->post('password')) == 32) {
            $datas['password'] = $this->input->post('password'); //密码
        } else {
            $datas['password'] = md5($this->input->post('password')); //密码
        }
        $datas['authority'] = $this->input->post('authority'); //管理员权限
        //$datas['last_login_time']   = date('Y-m-d H:i:s');  //上次登入时间,即将这次的登入时间记作下次用的上次登入时间
        //$datas['last_login_ip']  = $this->input->ip_address();  //上次登入ip,放在登录那一块儿做修改
        $datas['position'] = $this->input->post('position'); //管理位置

        return $datas;
    }

    /*
     *
    * 搜索信息
    *
    */
    function search()
    {
        if ($this->input->post('keywords') == '') {
            $where  = '';
            $limit  = '';
            $offset = '';
            $order  = 'id ASC';

            $data['result'] = $this->admin->get_list($where, $limit, $offset, $order);
        } else {
            $keywords = trim($this->input->post('keywords'));
            $super    = '超级后台';
            $normal   = '普通前台';
            $stop     = '暂被罢职前后台';
//0是前台管理员，普通管理员，1是后台管理员，是超级管理员，2是前后台管理员，是暂被罢职管理员
            if (strstr($super, $keywords)) {
                $keywords = 1;
            } else if (strstr($normal, $keywords)) {
                $keywords = 0;
            } else if (strstr($stop, $keywords)) {
                $keywords = 2;
            } else {
                $keywords = $this->input->post('keywords');
            }
            $like  = array($this->input->post('where') => $keywords);
            $order = 'id ' . $this->input->post('orderby');

            $data['result'] = $this->admin->get_list('', '', '', $order, $like);
        }
        $this->layout->view('admin/admin/admin_listV', $data);
    }

    function _upload_img($img_name, $img_url)
    {
        $config ['upload_path']   = './upload/'; //设置上传目录
        $config ['allowed_types'] = 'gif|jpg|png';
        $config ['max_size']      = 1024 * 2;
        $config ['encrypt_name']  = TRUE;

        $this->load->library('upload', $config);

        if ($_FILES[$img_name]['size'] && $this->upload->do_upload($img_name)) {

            if ($img_url != 'local_first_add') { //如果不是第一次增加图片，即是更改图片
                $url = './upload/' . $img_url;
                unlink($url); //删除原图
            }

            //取得上传成功数据
            $data = $this->upload->data();

            $img = $data['file_name'];
        } else {
            $img = $this->input->post($img_name); //图片
        }

        return $img;
    }
}